package com.heima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.UserThreadLocal;
import com.heima.domain.Patient;
import com.heima.domain.Users;
import com.heima.mapper.PatientMapper;
import com.heima.mapper.UsersMapper;
import com.heima.service.IPatientService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.vo.PatientVo;
import com.heima.vo.PatientVo2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 就诊人 服务实现类
 * </p>
 *
 * @author 黑马程序员
 * @since 2023-01-29
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements IPatientService {
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    /**
     * 就诊人列表
     *
     * @return
     */
    @Override
    public List<PatientVo> getList() {
        Integer id = UserThreadLocal.get();
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getUserId,id);
        List<Patient> patients = patientMapper.selectList(wrapper);
        if (patients == null) {
            throw new RuntimeException("查询失败");
        }

        List<PatientVo> vo = new ArrayList<>();
        for (Patient patient : patients) {
            PatientVo Vo = new PatientVo();
            BeanUtils.copyProperties(patient, Vo);
            if (patient.getIsDefault() == 0) {
                Vo.setIsDefault(false);
            } else {
                Vo.setIsDefault(true);
            }
            Vo.setCardNumber(DesensitizedUtil.idCardNum(patient.getCardNumber(), 4, 4));

            vo.add(Vo);
        }

        return vo;
    }

    /**
     * 查询就诊人信息
     *
     * @param patientId
     * @return
     */
    @Override
    public Patient getByPatientId(String patientId) {
        if (patientId == null) {
            throw new RuntimeException("参数不能为空");
        }
        Patient patient = patientMapper.selectById(patientId);
        if (patient == null) {
            throw new RuntimeException("查询失败");
        }

        return patient;
    }

    /**
     * 删除
     *
     * @param patientId
     * @return
     */
    @Override
    public boolean delete(String patientId) {
        if (patientId == null) {
            throw new RuntimeException("参数不能为空");
        }

        int i = patientMapper.deleteById(patientId);
        if (i == 0) {
            throw new RuntimeException("删除失败");
        }
        redisTemplate.delete("zhou");
        return true;
    }

    /**
     * 新增或修改
     *
     * @param patientId
     * @return
     */
    @Override
    public boolean add(String patientId, PatientVo2 vo2) {
        Integer id = UserThreadLocal.get();
        Patient patient = new Patient();
        if (patientId == null) {
            BeanUtils.copyProperties(vo2, patient);
            if (vo2.getIsDefault()) {
                patientMapper.updatedefault(id);
                patient.setIsDefault(1);
            } else {
                patient.setIsDefault(0);
            }
            patient.setUserId(id);
            int insert = patientMapper.insert(patient);
            if (insert != 1) {
                return false;
            }
            redisTemplate.opsForValue().set("zhou", String.valueOf(patient));
        }
        if (vo2.getIsDefault()) {
            patientMapper.updatedefault(id);
        }
        LambdaQueryWrapper<Patient> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Patient::getId, patientId);
        BeanUtils.copyProperties(vo2, patient);
        Boolean isDefault = vo2.getIsDefault();
        if (isDefault) {
            patient.setIsDefault(1);
        } else {
            patient.setIsDefault(0);
        }
        int update = patientMapper.update(patient, wrapper);
        if (update != 1) {
            return false;
        }
        redisTemplate.opsForValue().set("zhou", String.valueOf(patient));
        return true;
    }
}
